﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopBLL;
using ShopModel;

public partial class controls_ctrlCustomerData : System.Web.UI.UserControl
{
    #region VARIABLES

    public enum ModeType
    {
        NewAccount,
        EditData,
        Address,
    }
    public ModeType mode;

    #endregion VARIABLES

    #region PROPERTIES

    #region Data Properties

    public string Password
    {
        get
        {
            return txtPassword.Text;
        }

        set
        {
            txtPassword.Text = value;
        }
    }

    public string Question
    {
        get
        {
            return txtQuestion.Text;
        }

        set
        {
            txtQuestion.Text = value;
        }
    }

    public string Answer
    {
        get
        {
            return txtAnswer.Text;
        }

        set
        {
            txtAnswer.Text = value;
        }
    }

    public string Name
    {
        get
        {
            return txtName.Text;
        }

        set
        {
            txtName.Text = value;
        }
    }

    public string Surname
    {
        get
        {
            return txtSurname.Text;
        }

        set
        {
            txtSurname.Text = value;
        }
    }

    public string Street
    {
        get
        {
            return txtStreet.Text;
        }

        set
        {
            txtStreet.Text = value;
        }
    }

    public string Building
    {
        get
        {
            return txtBuilding.Text;
        }

        set
        {
            txtBuilding.Text = value;
        }
    }

    public string Flat
    {
        get
        {
            return txtFlat.Text;
        }

        set
        {
            txtFlat.Text = value;
        }
    }

    public string PostCode1
    {
        get
        {
            return txtPostCode1.Text;
        }

        set
        {
            txtPostCode1.Text = value;
        }
    }

    public string City
    {
        get
        {
            return txtCity.Text;
        }

        set
        {
            txtCity.Text = value;
        }
    }

    public string Email
    {
        get
        {
            if (Mode != ModeType.Address)
            {
                return txtEMail.Text;
            }
            else
            {
                return txtEmail2.Text;
            }
        }

        set
        {
            if (Mode != ModeType.Address)
            {
                txtEMail.Text = value;
            }
            else
            {
                txtEmail2.Text = value;
            }
        }
    }

    public string Phone
    {
        get
        {
            return txtPhoneNumber.Text;
        }

        set
        {
            txtPhoneNumber.Text = value;
        }
    }

  public int CountryID
    {
        get
        {
            int result = -1;
            if (!int.TryParse(ddlCountry.SelectedValue, out result))
            {
                result = -1;
            }
            return result;
        }

        set
        {
            ddlCountry.SelectedValue = value.ToString();
            
        }
    }


    public string CountryName
    {
        get
        {
            if (ddlCountry.SelectedItem != null)
            {
                return ddlCountry.SelectedItem.Text;
            }
            else
            {
                return string.Empty;
            }
        }
    }

    public string CompanyName
    {
        get
        {
            return txtCompanyName.Text;
        }

        set
        {
            txtCompanyName.Text = value;
        }
    }

    public string NIP
    {
        get
        {
            return txtNIP.Text;
        }

        set
        {
            txtNIP.Text = value;
        }
    }

    public bool OtherAddress
    {
        get
        {
            return chbOtherAddress.Checked;
        }

        set
        {
            chbOtherAddress.Checked = value;
        }
    }

    public int LanguageID
    {
        get
        {
            return int.Parse(ddlLanguage.SelectedValue);
        }

        set
        {
            ddlLanguage.SelectedValue = value.ToString();
        }
    }



    public string CompanyStreet
    {
        get
        {
            return txtCompanyStreet.Text;
        }

        set
        {
            txtCompanyStreet.Text = value;
        }
    }

    public string CompanyBuilding
    {
        get
        {
            return txtCompanyBuilding.Text;
        }

        set
        {
            txtCompanyBuilding.Text = value;
        }
    }

    public string CompanyFlat
    {
        get
        {
            return txtCompanyFlat.Text;
        }

        set
        {
            txtCompanyFlat.Text = value;
        }
    }

    public string CompanyPostCode1
    {
        get
        {
            return txtCompanyPostCode1.Text;
        }

        set
        {
            txtCompanyPostCode1.Text = value;
        }
    }

    public string CompanyCity
    {
        get
        {
            return txtCompanyCity.Text;
        }

        set
        {
            txtCompanyCity.Text = value;
        }
    }

    public bool SendAbroad
    {
        get
        {
            return chbAbroad.Checked;
        }

        set
        {
            chbAbroad.Checked = value;
        }
    }

    public bool Newsletter
    {
        get
        {
            return chbNewsletter.Checked;
        }

        set
        {
            chbNewsletter.Checked = value;
        }
    }


    public bool IsInvoice
    {
        get
        {
            return chbInvoice.Checked;
        }

        set
        {
            chbInvoice.Checked = value;
        }
    }

    public bool SameAddress
    {
        get
        {
            return chbSameAddress.Checked;
        }

        set
        {
            chbSameAddress.Checked = value;
        }
    }

    public string SendAbroadSelectedValue
    {
        get
        {
            return rblSendAbroad.SelectedValue;
        }

        set
        {
            rblSendAbroad.SelectedValue = value;
        }
    }

    public Customer Customer
    {
        get
        {
            Customer result = new Customer();
            result.Answer = Answer;
            result.City = City;
            result.CompanyCity = CompanyCity;
            result.CompanyName = CompanyName;
            result.CompanyNIP = NIP;
            result.CompanyStreet = CompanyStreet.Replace("/", string.Empty) + " " + CompanyBuilding.Replace("/", string.Empty).Replace(" ", string.Empty) + (string.IsNullOrEmpty(CompanyFlat) ? string.Empty : "/" + CompanyFlat.Replace("/", string.Empty).Replace(" ", string.Empty));
            result.CompanyZipCode = CompanyPostCode1;// +"-" + CompanyPostCode2;
            result.CountryID = CountryID;
            result.Country = CountryName;
            result.Email = Email;
            result.FirstName = Name;
            result.IsCompany = IsInvoice;
            result.Password = Password;
            result.Phone = Phone;
            result.PreferedLanguageID = (int)LanguageID;
            result.Question = Question;
            result.SameAddress = SameAddress;
            result.Street = Street.Replace("/", string.Empty) + " " + Building.Replace("/", string.Empty).Replace(" ", string.Empty) + (string.IsNullOrEmpty(Flat) ? string.Empty : "/" + Flat.Replace("/", string.Empty).Replace(" ", string.Empty));
            result.Surname = Surname;
            result.ZipCode = PostCode1;
            result.NewsSubscription = Newsletter;

            return result;
        }

        set
        {
            bool clear = true;
            string[] parts,partsn;

            Answer = value.Answer;
            City = value.City;
            CompanyCity = value.CompanyCity;
            CompanyName = value.CompanyName;
            NIP = value.CompanyNIP;
            CountryID = value.CountryID;
            Email = value.Email;
            Name = value.FirstName;
            IsInvoice = value.IsCompany;
            Password = value.Password;
            Phone = value.Phone;
            LanguageID = value.PreferedLanguageID;
            Question = value.Question;
            Newsletter = value.NewsSubscription;
            if (value.IsCompany)
            {
                SameAddress = value.SameAddress;
            }
            else
            {
                SameAddress = true;
            }
            Surname = value.Surname;

            chbAbroad.Checked = !string.IsNullOrEmpty(value.Country) && value.Country.ToLower() != "poland";

            if (!string.IsNullOrEmpty(value.Street))
            {
                parts = value.Street.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);

                Street = string.Empty;

                for (int i = 0; i < parts.Length - 1; i++)
                    Street += " " + parts[i];

                Street = Street.Trim();

                partsn = parts[parts.Length - 1].Split('/');
                Building = partsn[0];
                if (partsn.Length > 1)
                    Flat = partsn[1];
                else
                    Flat = string.Empty;
            }

            clear = false;
            if (!string.IsNullOrEmpty(value.CompanyStreet))
            {
                parts = value.CompanyStreet.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                CompanyStreet = string.Empty;

                for (int i = 0; i < parts.Length - 1; i++)
                    CompanyStreet += " " + parts[i];

                CompanyStreet = CompanyStreet.Trim();

                partsn = parts[parts.Length - 1].Split('/');
                CompanyBuilding = partsn[0];
                if (partsn.Length > 1)
                    CompanyFlat = partsn[1];
                else
                    CompanyFlat = string.Empty;
            }
            else
            {
                clear = true;
            }

            if (clear)
            {
                CompanyStreet = string.Empty;
                CompanyBuilding = string.Empty;
                CompanyFlat = string.Empty;
            }


            if (!string.IsNullOrEmpty(value.ZipCode))
            {
                PostCode1 = value.ZipCode;
            }

            clear = false;
            if (!string.IsNullOrEmpty(value.CompanyZipCode))
            {
                CompanyPostCode1 = value.CompanyZipCode;
            }
            else
            {
                clear = true;
            }

            if (clear)
            {
                CompanyPostCode1 = string.Empty;
            }
        }
    }

    #endregion

    #region Configuration Properties

    public bool ShowLabelInfo
    {
        set
        {
            ViewState["ctrlCustomerData_ShowLabel"] = value;
        }

        get
        {
            if (ViewState["ctrlCustomerData_ShowLabel"] != null)
            {
                return (bool)ViewState["ctrlCustomerData_ShowLabel"];
            }
            else
            {
                return true;
            }
        }
    }

    public ModeType Mode
    {
        set
        {
            ViewState["ctrlCustomerData_Mode"] = value;
            mode = value;
        }

        get
        {
            if (ViewState["ctrlCustomerData_Mode"] != null)
            {
                return (ModeType)ViewState["ctrlCustomerData_Mode"];
            }
            else
            {
                return mode;
            }
        }
    }

    #endregion

    #endregion PROPERTIES

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rblSendAbroad.Items.Add(new ListItem(Resources.Language.EuropeanUnion, "1"));
            rblSendAbroad.Items.Add(new ListItem(Resources.Language.OutsideEuropeanUnion, "2"));
            rblSendAbroad.Items[0].Selected = true;

            ddlLanguage.DataTextField = "Name";
            ddlLanguage.DataValueField = "LanguageID";
            ddlLanguage.DataSource = ReportsManager.Instance.GetAvailableLanguages(true);
            ddlLanguage.DataBind();

            ddlCountry.DataTextField = "Name";
            ddlCountry.DataValueField = "IDcountry";
            ddlCountry.DataSource = ReportsManager.Instance.GetCountries(CommonTools.CurrentLanguageID);
            ddlCountry.DataBind();

            ddlCountry.SelectedValue = Settings.DefaultCountry.ToString();

            if (chbInvoice.Checked)
            {
                valCompanyName.Enabled = true;
                valNIP.Enabled = true;

                if (!chbSameAddress.Checked)
                {
                    valCompanyPostCode1.Enabled = true;
                    valCompanyStreet.Enabled = true;
                    valCompanyCity.Enabled = true;
                    valCompanyBuilding.Enabled = true;
                }
                else
                {
                    valCompanyPostCode1.Enabled = false;
                    valCompanyStreet.Enabled = false;
                    valCompanyCity.Enabled = false;
                    valCompanyBuilding.Enabled = false;
                }
            }
            else
            {
                valCompanyName.Enabled = false;
                valNIP.Enabled = false;
                valCompanyPostCode1.Enabled = false;
                valCompanyStreet.Enabled = false;
                valCompanyCity.Enabled = false;
                valCompanyBuilding.Enabled = false;
            }
        }

        if (Mode == ModeType.Address)
        {
            valBuilding.Enabled = chbOtherAddress.Checked;
            valCity.Enabled = chbOtherAddress.Checked;
            valEmail2.Enabled = chbOtherAddress.Checked;
            valEmailFormat2.Enabled = chbOtherAddress.Checked;
            valName.Enabled = chbOtherAddress.Checked;
            valPhoneNumber.Enabled = chbOtherAddress.Checked;
            valPostCode1.Enabled = chbOtherAddress.Checked;
            valStreet.Enabled = chbOtherAddress.Checked;
            valSurname.Enabled = chbOtherAddress.Checked;
        }

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        switch (Mode)
        {

            case ModeType.NewAccount:
                lblOtherAddress.Text = Resources.Language.OrderConfirmation_NotLogIn;
                lblOtherAddress.Visible = ShowLabelInfo;
                pnlLoginSection.Visible = true;
                txtEmail2.Visible = false;
                lblEmail2.Visible = false;
                valEmail2.Visible = false;
                valEmailFormat2.Visible = false;
                pnlCustomerDataSection.Visible = true;
                rblSendAbroad.Visible = false;
                lblAbradSendInfo.Text = Resources.Language.CtrlCustomerDataShipmentMessage + ".";
                break;

            case ModeType.EditData:
                pnlLoginSection.Visible = false;
                txtEmail2.Visible = false;
                lblEmail2.Visible = false;
                valEmail2.Visible = false;
                valEmailFormat2.Visible = false;
                pnlCustomerDataSection.Visible = true;
                rblSendAbroad.Visible = false;
                lblAbradSendInfo.Text = Resources.Language.CtrlCustomerDataShipmentMessage + ".";
                break;

            case ModeType.Address:
                chbOtherAddress.Visible = true;
                lblOtherAddress.Text = Resources.Language.OrderConfirmation_LogIn;
                lblOtherAddress.Visible = ShowLabelInfo;
                pnlLoginSection.Visible = false;
                pnlNewClient.Style[HtmlTextWriterStyle.Display] = "none";
                txtEmail2.Visible = true;
                lblEmail2.Visible = true;
                valEmail2.Visible = true;
                valEmailFormat2.Visible = true;
                pnlCustomerDataSection.Visible = true;
                rblSendAbroad.Visible = false;
                chbInvoice.Visible = false;
                lblInvoice.Visible = false;
                lblAbradSendInfo.Text = Resources.Language.CtrlCustomerDataShipmentMessage + ".";
                break;
        }



        chbAbroad.Attributes["onClick"] += " ShowSendAbroadSection(this); ";
        chbInvoice.Attributes.Add("onClick", "ShowCompanySection(this);");
        chbSameAddress.Attributes.Add("onClick", "ShowCompanyAddressSection(this);");
        chbOtherAddress.Attributes["onClick"] += " SetAddressSectionVisibility(this,'" + pnlNewClient.ClientID + "');";
        if (this.Visible)
        {
            //"Javascript/ctrlCustomerData.js"
            string path = Request.ApplicationPath;
            if (path.EndsWith("/"))
            {
                path = path.Remove(path.Length - 1);
            }
            Page.ClientScript.RegisterClientScriptInclude("ctrlCustomerDataScript", path + "/Javascript/ctrlCustomerData.js");

            Page.ClientScript.RegisterStartupScript(this.GetType(), "StartScript", string.Format(
                "<script type=\"text/javascript\">ctrlCustomerData_SetElementOnLoad(\"{0}\", \"{1}\", \"{2}\", \"{3}\"," +
                "\"{4}\", \"{5}\", \"{6}\", \"{7}\", \"{8}\", \"{9}\", \"{10}\", \"{11}\", \"{12}\", \"{13}\"" +
                ", \"{14}\");" +
                "ctrlCustomerData_SetVisibilityOnLoad(\"{15}\", \"{16}\", \"{17}\");</script>",
                valCompanyName.ClientID, valNIP.ClientID, 
                valCompanyStreet.ClientID,
                valCompanyCity.ClientID, 
                valCompanyBuilding.ClientID, 
                valCompanyPostCode1.ClientID, 
                valName.ClientID, 
                valSurname.ClientID, 
                valStreet.ClientID, 
                valBuilding.ClientID, 
                valCity.ClientID,
                valEmail2.ClientID, 
                valEmailFormat2.ClientID, 
                valPhoneNumber.ClientID,
                valPostCode1.ClientID,
                chbAbroad.ClientID, chbInvoice.ClientID, chbSameAddress.ClientID));
        }

    }

    #endregion PAGE EVENTS
}
